import json
import random


# 获取指定校区名字的校区id，或者获取jgDepartmentId
def getSchoolId(rs, school_name):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    school_id = ''
    for l in rs_list:
        if l["name"] == school_name:
            school_id = l["id"]
    return school_id


# 随机获取员工userid
def getUserId(rs, is_teacher):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    user_id_list = []
    if is_teacher == 1:
        for l in rs_list:
            if l["isTeacher"] == 1:
                user_id = l["userId"]
                user_id_list.append(user_id)
        return random.choice(user_id_list)
    elif is_teacher == 0:
        for l in rs_list:
            if l["isTeacher"] == 0:
                user_id = l["userId"]
                user_id_list.append(user_id)
        return random.choice(user_id_list)


# 获取指定校区名字外的校区id，或者获取jgDepartmentId
def getSchoolId1(rs, school_name):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    school_id = ''
    for l in rs_list:
        if l["name"] == school_name:
            continue
        else:
            school_id = l["id"]
            break
    return school_id


# 获取数据库查询结果后，拼接成list，返回
def getList(rs, key):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    list = []
    for l in rs_list:
        list.append(l[key])
    return list


# 将list转化为string
def ListToStr(rs):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    temp_list = []
    for rl in rs_list:
        if isinstance(rl, int):
            temp_list.append(str(rl))
    if temp_list == []:
        s = ','.join(rs_list)
    else:
        s = ','.join(temp_list)
    return s


# 获取数据库查询结果后，拼接成list，再转化为string返回
def getListToStr(rs, key):
    rs_list = getList(rs, key)
    return ListToStr(rs_list)


#
def getSchoolId_t(rs, school_name="班级一"):
    school_id = ''
    for l in rs:
        if l["name"] == school_name:
            school_id = l["id"]
    return school_id


# 获取作为主教或助教的老师id
def getTeacherIdAsBishopOrAssistant(rs):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    teacherId = '0'
    for l in rs_list:
        if l["teachingClassNumAsBishop"] > 0 or l["teachingClassNumAsAssistant"] > 0:
            teacherId = l["teacherId"]
            break
    return teacherId


# 获取数据库查询结果后，拼接成list，返回值结果（转化为int型）
def getList_int(rs, key):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    list = []
    for l in rs_list:
        list.append(int(l[key]))
    return list






